package design.event.multi;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

import design.event.event.Event;

public class EventManager {
	private static final int EVENT_QUEUE_LENGTH = 1000;
	private static final int DISPATCHER_NUM = 2;
	private static final int EVENT_HANDLER_NUM = 10;

	public BlockingQueue<Event> eventQueue = new LinkedBlockingQueue<Event>(EVENT_QUEUE_LENGTH);
	private ExecutorService eventHandlerPool;

	protected EventDispatcher createDispatcher() {
		EventDispatcher dispatcher = new EventDispatcher(this.eventQueue, this.eventHandlerPool);
		dispatcher.register(EventType.CREATE, CreateEventHandler.class);
		return dispatcher;
	}

	public void init() {
		eventHandlerPool = Executors.newFixedThreadPool(EVENT_HANDLER_NUM);
	}

	public void start() {
		for (int i = 0; i < DISPATCHER_NUM; i++) {
			createDispatcher().start();
		}
	}

	public void notify(Event event) {
		try {
			eventQueue.put(event);
		} catch (InterruptedException e) {
		}
	}
}
